# -*- coding: utf-8 -*-
from django.db import models
from background_task import background
from django.contrib.auth.models import User

@background(schedule=60)
def notify_user(r_id):
    obj = Reminder.objects.get(id=r_id)
    users = obj.users.all()
    for user in users:
        user.email_user(obj.subject, obj.text)

class Reminder(models.Model):
    users = models.ManyToManyField(User, verbose_name=u"Кому отправить напоминание")
    subject = models.CharField(max_length=200, verbose_name=u"Тема напоминания")
    text = models.TextField(verbose_name=u"Текст напоминания")
    time = models.DateTimeField(verbose_name=u"Время напоминания")

    class Meta:
        verbose_name=u'Напоминие'
        verbose_name_plural = u'Напоминания'

    def save(self, *args, **kwargs):
        super(Reminder, self).save(*args, **kwargs)
        notify_user(self.id)





